﻿using eslib.tools.config;

namespace ArticleClientLib
{
    public class ArticleImageJson
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="fileName">文件名</param>
        /// <param name="extName">扩展名</param>
        /// <param name="buffer">缓存</param>
        public ArticleImageJson(string fileName, string extName, byte[] buffer)
        {
            this.fileName = fileName;
            this.extName = extName;
            this.buffer = buffer;
        }

        public string fileName { get; set; }

        public string extName { get; set; }

        public byte[] buffer { get; set; }


        public string ToJson()
        {
            return GenericJsonConfig.Save(this);
        }


        public static ArticleImageJson OpenImage(string fileName)
        {
            string fname = Path.GetFileNameWithoutExtension(fileName);
            string ext = Path.GetExtension(fileName).Replace(".", "");
            byte[] buf = File.ReadAllBytes(fileName);
            if (buf.Length >= 4096 * 1024) throw new Exception($"图片大小不得大于4MB，{fileName}");

            return new ArticleImageJson(fname, ext, buf);
        }
    }
}